<?php

namespace App\Admin\Controllers;

use App\City;
use App\Park;
use App\Http\Controllers\Controller;
use App\ParkArea;
use App\ParkFormat;
use App\ParkType;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class ParkController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('园区管理')
            ->description('园区管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('园区详情')
            ->description('园区详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('园区编辑')
            ->description('园区编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('园区创建')
            ->description('园区创建')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Park);
        $grid->model()->orderBy('id','desc');
        $grid->id('Id');
        $grid->area('园区面积');

        $grid->code('园区编号');
        $grid->park_type_id('园区类型')->display(function ($value){
            return ParkType::find($value)->name ?? '';
        });
        $grid->title('园区名称');
        $grid->park_format_id('园区业态')->display(function ($value){
            return ParkFormat::find($value)->name ?? '' ;
        });
        $grid->city_id('城市')->display(function ($value){
            return City::find($value)->title ?? '';
        });
        $grid->area_id('区域')->display(function ($value){
            return City::find($value)->title ?? '';
        });
        $grid->is_hot('热度')->display(function($is_hot){
        	return $is_hot ? '热' : '否';
        });
        //$grid->des('园区概况');
        $grid->column('guanli','园区管理')->display(function($vlaue){
            return "<a href='/admin/parkBaner?park_id={$this->id}' target='_self'> 编辑</a>";
        });
        $grid->column('yulan','预览')->display(function($vlaue){
            return "<a href='".config('admin.app_outurl')."/#/park/{$this->id}' target='_blank'> 查看</a>";
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Park::findOrFail($id));

        $show->id('Id');
        $show->area('园区面积');
        $show->code('园区编号');
        $show->park_type_id('园区类型');
        $show->title('园区名称');
        $show->park_format_id('园区业态');
        $show->city_id('区域');
        $show->is_hot('热度');
        $show->des('园区概况');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Park);

        $form->uploader('slider','轮播图');

        $form->tags('tag', '标签')->help('添加输入后点击空白处');
        $form->currency('area', '园区面积')->symbol("㎡");
        $form->select('park_area_id', '面积范围')->options(ParkArea::getSelectOptions())->rules('required', [
            'required' => '请选择面积范围'
        ]);
        $form->select('park_type_id', '园区类型')->options(ParkType::getSelectOptions())->rules('required',['required'=>'请选择园区类型']);
        $form->text('title', '园区名称')->rules('required',[
            'required'=>'请输入园区名称'
        ]);
        $form->select('park_format_id', '园区业态')->options(ParkFormat::getSelectOptions())->rules('required',[
            'required'=>'请选择园区业态'
        ]);
        $form->hidden('code','园区编号')->default(Carbon::now()->timestamp);
        $form->select('city_id', '城市')->options(City::getSelectOptions())->load('area_id','/admin/api/area')->rules('required',[
            'required'=>'请选择城市'
        ]);
        $form->select('area_id','区域')->options(function($id){
            return City::option($id);
        })->load('area1_id','/admin/api/area1')->rules('required',[
            'required'=>'请选择区域'
        ]);
        $form->select('area1_id','下级区域')->options(function($id){
            return City::option($id);
        });
        $hots = [
	        1=>'热',
	        0=>'否',
    	];
        //$form->map('latitude','longitude','地址与周边设施');
        $form->ckeditor('des', '园区概况');
        $form->select('is_hot','热度')->options($hots);
        $form->lceditor('bottom', '自定义底部');
        return $form;
    }
    public function  area(Request $request ){
        $provinceId = $request->get('q');
        return City::where('parent_id', $provinceId)->get(['id', \DB::raw('title as text')]);

    }
    public function  area1(Request $request ){
        $provinceId = $request->get('q');
        return City::where('parent_id', $provinceId)->get(['id', \DB::raw('title as text')]);

    }
}
